package com.shm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.ProvinceCityRegion;
import com.ruoyi.common.core.domain.vo.AreaItemVo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.shm.service.IProvinceCityRegionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 省-市-区Controller
 *
 * @author dam
 * @date 2023-07-15
 */
@RestController
@RequestMapping("/schoolMessage/provinceCityRegion")
public class ProvinceCityRegionController extends BaseController {
    @Autowired
    private IProvinceCityRegionService provinceCityRegionService;

    /**
     * 获取省市区的树形结构数据
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:getAreaTree')")
    @GetMapping("/getAreaTree")
    public AjaxResult getAreaTree() {
        List<AreaItemVo> areaItemVoList = provinceCityRegionService.getAreaTree();
        return AjaxResult.success("areaItemVoList", areaItemVoList);
    }


    /**
     * 查询省-市-区列表
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProvinceCityRegion provinceCityRegion) {
        startPage();
        List<ProvinceCityRegion> list = provinceCityRegionService.selectProvinceCityRegionList(provinceCityRegion);
        return getDataTable(list);
    }

    /**
     * 根据省id查询出该省所有区
     *
     * @param provinceId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:list')")
    @GetMapping("/listRegionByProvinceId")
    public TableDataInfo listRegionByProvinceId(Long provinceId) {
        List<ProvinceCityRegion> list = provinceCityRegionService.listRegionByProvinceId(provinceId);
        return getDataTable(list);
    }

    /**
     * 导出省-市-区列表
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:export')")
    @Log(title = "省-市-区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProvinceCityRegion provinceCityRegion) {
        List<ProvinceCityRegion> list = provinceCityRegionService.selectProvinceCityRegionList(provinceCityRegion);
        ExcelUtil<ProvinceCityRegion> util = new ExcelUtil<ProvinceCityRegion>(ProvinceCityRegion.class);
        util.exportExcel(response, list, "省-市-区数据");
    }

    /**
     * 获取省-市-区详细信息
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(provinceCityRegionService.selectProvinceCityRegionById(id));
    }

    /**
     * 新增省-市-区
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:add')")
    @Log(title = "省-市-区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProvinceCityRegion provinceCityRegion) {
        return toAjax(provinceCityRegionService.insertProvinceCityRegion(provinceCityRegion));
    }

    /**
     * 修改省-市-区
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:edit')")
    @Log(title = "省-市-区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProvinceCityRegion provinceCityRegion) {
        return toAjax(provinceCityRegionService.updateProvinceCityRegion(provinceCityRegion));
    }

    /**
     * 删除省-市-区
     */
    @PreAuthorize("@ss.hasPermi('schoolMessage:provinceCityRegion:remove')")
    @Log(title = "省-市-区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(provinceCityRegionService.deleteProvinceCityRegionByIds(ids));
    }
}
